﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ATPlatforms.RabbitSkin.Serializers;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using RabbitMQ.Client.MessagePatterns;

namespace ATPlatforms.RabbitSkin
{
    public class RpcMethod : BaseProducerConsumer
    {
        protected string MethodName { get; set; }
        protected RpcMethodHander mMethodHandler;
      
        public RpcMethod(string server, string methodName, RpcMethodHander handler)
            : base(server, "", ExchangeType.Direct)
        {
            MethodName = methodName;
            mMethodHandler = handler;
        }

        public override bool StartBasic()
        {
            if (base.StartBasic())
            {
                mQueue = Model.QueueDeclare(MethodName);
                //Model.QueueBind(mQueue, Exchange, MethodName, false, null);
                QueueWorkDelegate c = new QueueWorkDelegate(Consume);
                c.BeginInvoke(null, null);
                return true;
            }
            return false;
        }

        private void Consume()
        {
            mSubscription = new Subscription(Model, mQueue, false);
            mMethodHandler.SetSubscription(mSubscription);
            mMethodHandler.MainLoop();
        }
    }
}
